const logger = require('../logger');
const db = require('../../db');

class StorageModel {
    /*
    constructor() {

    }
    */

    /**
     * 模型列表
     * @param {*} req
     */
    getStorageModelList(req) {
        let model_id = '';
        let condition = {};
        if (req.params) {
            model_id = req.params.model_id;
            if (model_id) {
                condition = { model_id };
            }
        }
        logger.data.info('model.storageModel.getStorageModelList req：', req.params);
        return new Promise(function (resolve, reject) {
            db.crawler_storage_model.findAndCountAll({
                // attributes: [],
                where: condition,
                order: [
                    ['model_id', 'desc']
                ],
            }).then(function (result) {
                resolve(result);
            }, function (err) {
                logger.error.error('model.storageModel.getStorageModelList error：', err);
                reject(err);
            });
        });
    }

    /**
     * 保存落地模型
     */
    saveStorageModel(req) {
        let reqData = req.body;
        let { model_id } = reqData;
        logger.data.info('model.storageModel.saveStorageModel req：', reqData);
        return new Promise(function (resolve, reject) {
            if (Number(model_id)) {
                logger.data.info('model.storageModel.saveStorageModel update：', reqData);
                db.crawler_storage_model.update(reqData, {
                    where: { model_id },
                }).then(result => {
                    resolve(result[0]);
                }).catch(e => {
                    logger.error.error('model.storageModel.saveStorageModel update error：', e);
                    resolve('');
                });
            } else {
                // 下面这些字段不必填，给默认值
                reqData.status = 1;
                logger.data.info('model.storageModel.saveStorageModel create：', reqData);
                db.crawler_storage_model.create(reqData).then(result => {
                    resolve(result);
                }).catch(e => {
                    logger.error.error('model.storageModel.saveStorageModel create error：', e);
                    resolve('');
                });
            }
        });
    }

    /**
     * 删除模型（物理删除）
     */
    deleteStorageModel(req) {
        let { model_id } = req.params;
        const uid = req.userInfo.uid;
        return new Promise(function (resolve, reject) {
            if (Number(model_id)) {
                logger.data.info('model.storageModel.deleteStorageModel：', uid, model_id);
                db.crawler_storage_model.destroy({
                    where: { model_id }
                }).then(result => {
                    resolve(result);
                }).catch(e => {
                    logger.error.error('model.storageModel.deleteStorageModel：', e);
                    resolve('');
                });
            } else {
                logger.error.error('model.storageModel.deleteStorageModel：model_id is null');
            }
        });
    }
}


module.exports = new StorageModel();
